var $zxd = {
    v: {
        ajaxOption: {method: 'get', dataType: 'json', async: true}
    },
    notify: function (msg, status) {
        if (status == 'error') {
            toastr.error(msg);
        } else {
            toastr.success(msg);
        }
    },
    uiform: function () {
        jQuery('tbody input:checkbox').click(function () {
            if (jQuery(this).is(':checked')) {
                jQuery(this).parent().addClass('checked');
                jQuery(this).parents('tr').addClass('warning');
            } else {
                jQuery(this).parent().removeClass('checked');
                jQuery(this).parents('tr').removeClass('warning');
            }
        });
    },
    cutText: function (sub, length, less) {
        var str = "";
        if (sub && sub.length > length) {
            str = sub.substr(0, length);
            if (less) {
                str = str + less;
                str = '<p title="' + sub + '">' + str + '</p>';
            }
        } else {
            str = sub;
        }

        return str;
    },
    ajax: function (url, data, callbackFun, option) {
        if (option == null || option == undefined) {
            option = $zxd.v.ajaxOption;
        } else {
            if (option.method == null || option.method == undefined) {
                option.method = $zxd.v.ajaxOption.method;
            }
            if (option.dataType == null || option.dataType == undefined) {
                option.dataType = $zxd.v.ajaxOption.dataType;
            }
            if (option.async == null || option.async == undefined) {
                option.async = $zxd.v.ajaxOption.async;
            }
        }
        jQuery.ajax({
            dataType: option.dataType,
            url: url,
            data: data,
            async: option.async,
            success: function (data) {
                callbackFun(data);
            },
            statusCode: {
                401: function () {
                },
                403: function () {
                },
                500: function () {
                }
            }
        });
    },
    /**
     * 清理表单参数
     * @param form
     * @param option boolean类型，为true清理select插件
     */
    clearForm: function (form) {
        form.find("textarea").val("");
        form.find("input").val("");
        form.find("input[type=checkbox]").removeAttr("checked");
    },
    // 复选框的全选和反选
    checkAll: function () {
        $("input[name='checkbox']").each(function () {
            if ($(this).attr("checked")) {
                $(this).removeAttr("checked");
            } else {
                $(this).attr("checked", "true");
            }
        })
    }
};

(function ($) {
    $.fn.getInputId = function (sigle) {
        var checkIds = [];

        this.each(function () {
            checkIds.push($(this).val())
        });

        if (sigle) {
            if (checkIds.length > 1) {
                $zxd.notify('只能选择一条记录！', 'error');
                return false;
            }
            else if (checkIds.length == 0) {
                $zxd.notify('请选择一条记录操作！', 'error');
                return false;
            }
            else {
                return checkIds[0];
            }
        } else {
            if (checkIds.length == 0) {
                $zxd.notify('请选择至少一条记录操作！', 'error');
                return false;
            } else {
                return checkIds;
            }
        }
    };
})(jQuery);